<?php
namespace App\Http\Controllers\ChinaBank;

use Illuminate\Http\Request;
use EasyWeChat;
use App\Models\UserModel;



class UserController extends Controller
{

    public function login(Request $request){

        $code           = $request->input('code');
        $miniProgram    = EasyWeChat::miniProgram();
        $userInfo       = $miniProgram->auth->session($code);
        $openId         = $userInfo['openid'];
        $user           = UserModel::findUserByOpenid($openId);

        if(!$user){
            $user   = UserModel::create([
                "openid"    => $openId,
                "head"      => $request->input('head'),
                "wx_name"   => $request->input('wxName'),
            ]);
        }

        return apires(['user'=>$user]);
    }
}
